<template>
  <div class="app-container">
    <div>
      <avue-form
        :option="option"
        v-model="queryParams"
        ref="searchForm"
        v-show="showSearch"
        @keydown.enter="getList"
      >
        <template #menu-form>
          <el-button icon="Search" type="primary" @click="getList" class="m-l-3"
            >查询</el-button
          >
          <el-button icon="Refresh" @click="handleEmpty(searchForm)"
            >重置</el-button
          >
        </template>
      </avue-form>

      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="EditPen"
            @click="handleUpdate"
            :disabled="disUpdate"
            >编辑申请</el-button
          >
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="EditPen"
            @click="handleDel"
            :disabled="disDelete"
            >删除</el-button
          >
        </el-col>
      </el-row>

      <el-table
        v-loading="loading"
        :data="tableList"
        row-key="id"
        stripe
        row-class-name="cursor-pointer"
        @selection-change="handleSelectionChange"
        @row-dblclick="
          row => {
            goTo('/source/sourceModules/evaluateDetails', {
              evaluateId: row.id,
              proInsId: row.proInsId,
              isCirculation:
                row.approvalStatus === '审批通过' ? true : undefined
            })
          }
        "
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column prop="supplierName" label="供应商名称" />
        <el-table-column
          prop="supplierTaxcode"
          label="统一社会信用代码"
          width="180"
        />
        <el-table-column prop="annual" label="申请年度" width="130" />
        <el-table-column prop="createTime" label="申请时间" width="130">
          <template #default="scope">
            <div>
              {{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="来源" width="130">
          <template #default="scope">
            <div>
              <dict-drop :options="supplier_type" :value="scope.row.type" />
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="approvalStatus" label="状态" width="130" />
        <el-table-column prop="orgName" label="所属公司" width="130" />
        <el-table-column prop="sumScore" label="评定结果" width="130">
          <template #default="scope">
            <div>
              {{ scoreRes(scope.row.sumScore) }}
            </div>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="status" label="状态" width="130">
          <template #default="scope">
            <div>
              <dict-drop :options="order_status" :value="scope.row.status" />
              <span
                class="text-12px text-#999999"
                v-if="scope.row.approvalStatus"
                >({{ scope.row.approvalStatus }})</span
              >
            </div>
          </template>
        </el-table-column> -->
        <!-- <el-table-column
          label="操作"
          align="center"
          class-name="small-padding"
          min-width="90"
        >
          <template #default="scope">
            <el-button
              plain
              type="primary"
              icon="CircleCheckFilled"
              size="small"
              class="px-11px"
              @click="handleContinue(scope.row)"
              >跟进</el-button
            >
          </template>
        </el-table-column> -->
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>
<script setup name="EvaluateTable">
import { useRouter } from 'vue-router'
import { listEvaluate, delEvaluate } from '@/api/supplierManage/index'
import {
  companyOption //归属公司
} from '@/hooks/dict/order'
import useUserStore from '@/store/modules/user'
const userStore = useUserStore()
const router = useRouter()
const { proxy } = getCurrentInstance()

const { yes_or_no, sys_have_no, supplier_type } = proxy.useDict(
  'yes_or_no', //是否
  'sys_have_no', //有无
  'supplier_type'
)

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    supplierName: undefined,
    orgName: userStore.userInfo.orgId,
    supplierTaxcode: undefined,
    expireDate: undefined
  },
  ids: [],
  selectArr: [],
  total: 0,
  option: {
    labelWidth: '10',
    menuSpan: 6,
    submitBtn: false,
    menuPosition: 'left',
    // gutter: 30,
    emptyBtn: false,
    column: [
      {
        prop: 'supplierName',
        span: 3,
        placeholder: '供应商名称',
        type: 'input'
      },
      {
        prop: 'supplierTaxcode',
        span: 3,
        placeholder: '供应商税号',
        type: 'input'
      },
      {
        prop: 'type',
        span: 3,
        placeholder: '供应商来源',
        type: 'select',
        dicData: supplier_type
      },
      {
        prop: 'orgName',
        span: 3,
        placeholder: '所属公司',
        type: 'tree',
        defaultExpandAll: true,
        disabled: userStore.userInfo.orgId != 484,
        props: {
          value: 'id', //   value: 'deptId',
          label: 'label' //   label: 'deptName'
        },
        dicData: companyOption
      }
      // {
      //   prop: 'expireDate',
      //   type: 'daterange',
      //   span: 5,
      //   format: 'YYYY-MM-DD',
      //   valueFormat: 'YYYY-MM-DD',
      //   startPlaceholder: '到期开始时间',
      //   endPlaceholder: '到期结束时间'
      // }
    ]
  },
  showSearch: true,
  disUpdate: true /* 编辑按钮是否可编辑 */,
  disDelete: true /* 关闭按钮是否可编辑 */,
  loading: false,
  tableList: [],
  followUpParam: {} // 跟进弹窗配置
})
const {
  queryParams,
  ids,
  selectArr,
  total,
  option,
  showSearch,
  disUpdate,
  disDelete,
  loading,
  tableList,
  followUpParam
} = toRefs(data)

const scoreRes = computed(() => {
  return val => {
    let res = '结果'
    switch (true) {
      case val >= 90:
        res = '优秀'
        break
      case 80 <= val && val < 90:
        res = '良好'
        break
      case 60 <= val && val < 80:
        res = '合格'
        break

      default:
        res = '不合格'
        break
    }
    return res
  }
})

// 初始化
async function init() {
  // 归属部门
  getList()
}
// 重置
function handleEmpty(formEl) {
  console.log('queryParams.value1==>', toRaw(queryParams.value))
  proxy.resetForm('searchForm')
  proxy.resetForm('seniorSearchForm')
  // proxy.$refs.searchForm.resetForm()
  getList()
}

// 获取数据列表
function getList() {
  loading.value = true
  console.log('queryParams.value2==>', queryParams.value)
  listEvaluate(queryParams.value).then(res => {
    loading.value = false
    tableList.value = res.rows
    total.value = res.total
    console.log(' tableList.value==>', tableList.value)
  })
  console.log('获取数据')
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  selectArr.value = selection
  let approvalUpdate = selectArr.value.some(
    item => item.approvalStatus == '流程未发起'
  )
  // let approvalStatus = selectArr.value.some(
  //   item => item.approvalStatus == '审批通过'
  // )
  let dele = selectArr.value.some(
    item => item.createBy !== userStore.userInfo.userName
  )
  let roles =
    userStore.roles.includes('admin') || userStore.roles.includes('swgl')
  disUpdate.value = selection.length != 1 || !approvalUpdate
  disDelete.value = !selection.length || roles ? false : dele
}
function goTo(url, data) {
  router.push({ path: url, query: data })
}
function handleUpdate() {
  goTo('/source/sourceModules/editEvaluate', {
    evaluateId: ids.value[0]
  })
}

// 删除
function handleDel(row) {
  // let rowIds = row.id
  let supplierName = selectArr.value.map(item => item.supplierName)
  proxy
    .$confirm(`是否将“${supplierName.toString()}”从列表中删除`, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    })
    .then(() => {
      delEvaluate(ids.value.toString()).then(() => {
        getList()
        proxy.$message({
          type: 'success',
          message: '移出成功!'
        })
      })
    })
    .catch(() => {})
}

init()
</script>
<style scoped>
.search-drawer-retract {
  color: #999999;
}
</style>
